<template>
  <section>
    <el-form ref="form" :rules="rules" :model="currentTask" label-width="100px" class="demo-form">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-card class="box-card">
            <el-form-item label="任务标题" prop="title">
              <el-input v-model="currentTask.title"></el-input>
            </el-form-item>
            <el-form-item label="下发人员">
              <el-select v-model="userType" placeholder="请选择" style="width: 300px;">
                <el-option
                  v-for="item in UserTypes"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="企业名称" v-if="currentTask.userType !== 'dz' ">
              <depart-selector v-model="currentTask.departName" @change="departSelected"></depart-selector>
            </el-form-item>
            <el-form-item label="企业类别" v-if="currentTask.userType !== 'dz' ">
              <depart-category-selector v-model="currentTask.departCategoryName" @change="departCategorySelected"></depart-category-selector>
            </el-form-item>
            <el-form-item label="下发类型">
              <el-select v-model="currentTask.questType" placeholder="请选择" style="width: 300px;" @change="selectQuesType">
                <el-option
                  v-for="item in QuestTypes"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="普通题目" v-if="currentTask.questType==='General'">
              <el-card >
                <template v-for="(domain, index) in currentTask.quests" >
                    <el-row :key="index" :gutter="2" style="border-bottom:1px solid #cecece;padding-bottom: 5px;padding-top: 5px; ">
                      <el-col :span="22">
                        <span>{{index+1}}：</span>
                        <span>{{domain.title}}</span>
                      </el-col>
                      <el-col :span="2">
                        <el-button @click.prevent="removeOption(domain)">删除</el-button>
                      </el-col>
                    </el-row>
                </template>
                  <el-row style="text-align: center;margin-top: 5px;">
                    <el-col>
                        <span >
                        <el-button @click="addQuestion">添加</el-button>
                    </span>
                    </el-col>
                  </el-row>
              </el-card>
            </el-form-item>

            <el-form-item label="视频题目" v-if="currentTask.questType==='Video'">
              <el-card >
                <template v-for="(domain, index) in currentTask.quests" >
                  <el-row :key="index" :gutter="2" style="border-bottom:1px solid #cecece;padding-bottom: 5px;padding-top: 5px; ">
                    <el-col :span="22">
                      <span>{{index+1}}：</span>
                      <span>{{domain.title}}</span>
                    </el-col>
                    <el-col :span="2">
                      <el-button @click.prevent="removeOption(domain)">删除</el-button>
                    </el-col>
                  </el-row>
                </template>
                <el-row style="text-align: center;margin-top: 5px;" v-if="currentTask.quests.length===0">
                  <el-col>
                        <span >
                        <el-button @click="addVideo">添加</el-button>
                    </span>
                  </el-col>
                </el-row>
              </el-card>
            </el-form-item>
            <el-form-item label="新闻资讯" v-if="currentTask.questType==='News'">
              <el-card >
                <template v-for="(domain, index) in currentTask.quests" >
                  <el-row :key="index" :gutter="2" style="border-bottom:1px solid #cecece;padding-bottom: 5px;padding-top: 5px; ">
                    <el-col :span="22">
                      <span>{{index+1}}：</span>
                      <span>{{domain.title}}</span>
                    </el-col>
                    <el-col :span="2">
                      <el-button @click.prevent="removeOption(domain)">删除</el-button>
                    </el-col>
                  </el-row>
                </template>
                <el-row style="text-align: center;margin-top: 5px;" v-if="currentTask.quests.length===0">
                  <el-col>
                        <span >
                        <el-button @click="addNews">添加</el-button>
                    </span>
                  </el-col>
                </el-row>
              </el-card>
            </el-form-item>

            <el-form-item label="任务时间" style="width: 300px;">
              <el-date-picker
                v-model="value4"
                type="daterange"
                range-separator="至"
                start-placeholder="下发时间"
                end-placeholder="截止时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
            <span style="float: right;">
                <el-button type="primary"  @click="save('form',0)">保存</el-button>
                <!--<el-button type="primary"  @click="save('form',1)">发送任务</el-button>-->
                <el-button @click="back">返回</el-button>
            </span>

            </el-form-item>
          </el-card>
        </el-col>
      </el-row>


    </el-form>

    <el-dialog :visible.sync="showSelectQuestion" title="选择普通题库" width="70%"  append-to-body>
      <select-question @close="onShowSelectQuestionClose" :type="currentTask.userType" :refresh.sync="showSelectQuestion" :is-close="showSelectQuestion"></select-question>
    </el-dialog>

    <el-dialog :visible.sync="showSelectVideo" title="选择视频题库" width="70%"  append-to-body>
      <select-video @close="onShowSelectVideoClose" :refresh="showSelectVideo" :is-close="showSelectVideo"></select-video>
    </el-dialog>
    <el-dialog :visible.sync="showSelectNews" title="选择资讯" width="70%"  append-to-body>
      <select-news @close="onShowSelectNewsClose" :refresh="showSelectNews" :is-close="showSelectNews"></select-news>
    </el-dialog>
  </section>

</template>
<script>
import { mapGetters } from "vuex";
import Editor from "@/components/editor";
import ImageUploader from "@/components/image-uploader";
import MediaUploader from "@/components/media-uploader";
import Status from "@/libs/status";
import SelectQuestion from "./select-question";
import SelectVideo from "./select-video";
import SelectNews from "./select-news";
import Moment from "moment";
import DepartSelector from "@/components/depart-selector";
import DepartCategorySelector from "../../../components/depart-category-selector/index";
export default {
  props: {
    isClose: Boolean
  },
  created() {},
  mounted() {
    this.$watch("currentTask", val => {
      if (val.id) {
        this.value4 = [val.startTime, val.endTime];
      } else {
        this.value4 = [];
      }
    });
  },
  computed: {
    ...mapGetters({
      currentTask: "currentTask"
    }),
    userType() {
      if (!this.currentTask.userType) {
        return "all";
      } else {
        return this.currentTask.userType;
      }
    }
  },
  data() {
    return {
      value4: [],
      showSelectVideo: false,
      showSelectQuestion: false,
      showSelectNews: false,
      searchQuestion: { title: "", type: "" },
      UserTypes: Status.UserTypes,
      QuestTypes: Status.QuestTypes,
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { min: 3, max: 50, message: "长度在 3 到 50 个字符", trigger: "blur" }
        ],
        score: [{ required: true, message: "请输入积分", trigger: "blur" }]
      }
    };
  },
  methods: {
    departSelected(id, name) {
      this.currentTask.departId = id;
      this.currentTask.departName = name;
    },
    departCategorySelected(id, name) {
      this.currentTask.departCategoryId = id;
      this.currentTask.departCategoryName = name;
    },
    getProp(index) {
      return "quests." + index + ".id";
    },
    selectQuesType(val) {
      this.$store.dispatch("clearQuests");
    },
    addQuestion() {
      this.showSelectQuestion = true;
    },
    addVideo() {
      this.showSelectVideo = true;
    },
    addNews() {
      this.showSelectNews = true;
    },
    removeOption(item) {
      var index = this.currentTask.quests.indexOf(item);
      if (index !== -1) {
        this.currentTask.quests.splice(index, 1);
      }
    },
    onShowSelectQuestionClose() {
      this.showSelectQuestion = false;
    },
    onShowSelectVideoClose() {
      this.showSelectVideo = false;
    },
    onShowSelectNewsClose() {
      this.showSelectNews = false;
    },
    save(formName, status) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.currentTask.userType === "all") {
            this.currentTask.userType = null;
          }
          if (this.currentTask.userType === "dz") {
            this.currentTask.departId = null;
            this.currentTask.departName = null;
            this.currentTask.departCategoryId = null;
            this.currentTask.departCategoryName = null;
          }
          if (!this.currentTask.departName) {
            this.currentTask.departId = null;
          }
          if (!this.currentTask.departCategoryName) {
            this.currentTask.departCategoryId = null;
          }
          this.currentTask.startTime = "";
          this.currentTask.endTime = "";
          // this.currentTask.status = status;
          if (this.value4 && this.value4.length > 0) {
            this.currentTask.startTime = Moment(this.value4[0]).format(
              "YYYY-MM-DD HH:mm:ss"
            );
            this.currentTask.endTime = Moment(this.value4[1]).format(
              "YYYY-MM-DD HH:mm:ss"
            );
          }
          this.$store
            .dispatch("addOrUpdateTaskS", this.currentTask)
            .then(ret => {
              this.$notify({
                title: "添加成功",
                type: "success"
              });
              this.back();
            });
        } else {
          return false;
        }
      });
    },
    back() {
      this.$emit("close");
    }
  },
  components: {
    Editor,
    ImageUploader,
    MediaUploader,
    SelectQuestion,
    SelectVideo,
    SelectNews,
    DepartSelector,
    DepartCategorySelector
  }
};
</script>
<style>
</style>
